package com.shenluyao.contrller;


import com.shenluyao.common.HxConstants;
import com.shenluyao.common.ResultDTO;
import com.shenluyao.model.FpsPerMasterData;
import com.shenluyao.service.FpsPerMasterDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author SHIXIAOFEI
 * @Description: 组织结构
 * @date 2018/11/19 10:44
 */
@RestController
@RequestMapping(value = "/lyen/httpServermaster")
public class FpsPerMasterDataController {
    @Autowired
    private FpsPerMasterDataService fpsPerMasterDataService;

    /**
     * 远程新增组织结构
     * @param baseEntity
     * @return
     */
    @RequestMapping(value = "/remoteCreateMaster.xhdx",method= RequestMethod.GET)
    @ResponseBody
    public ResultDTO<?> create(FpsPerMasterData baseEntity){
        ResultDTO<FpsPerMasterData> resultDTO = new ResultDTO<>();
        if(baseEntity==null){
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_PARAMETER_NULL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_PARAMETER_NULL);
            return resultDTO;
        }
        String id = baseEntity.getId();
        String name = baseEntity.getName();
        String code = baseEntity.getCode();
        if (null == id || "".equals(id)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("id为不能空，必须输入！");
            return resultDTO;
        }
        if (null == name || "".equals(name)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("name不能为空，必须输入！");
            return resultDTO;
        }
        if (null == code || "".equals(code)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("code不能为空，必须输入！");
            return resultDTO;
        }
        try{
            FpsPerMasterData fpsPerMasterData = fpsPerMasterDataService.getFpsPerMasterDataById(baseEntity.getId());
            if (fpsPerMasterData != null){
                resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_EXIST);
                resultDTO.setReturnMsg("该数据的数据id已存在，请查证！");
                return resultDTO;
            }
            FpsPerMasterData fmd = new FpsPerMasterData();
            fmd.setName(baseEntity.getName());
            fmd.setCode(baseEntity.getCode());
            List<FpsPerMasterData> fp = fpsPerMasterDataService.findFpsPerMasterData(fmd);
            if(fp!=null && fp.size()>0){
                resultDTO.setReturnCode(HxConstants.RETURN_CODE_DATA_EXIST);
                resultDTO.setReturnMsg("name和code的组织机构已存在，请查证");
                return resultDTO;
            }
            String parentId = baseEntity.getParentid();
            if(StringUtils.isEmpty(parentId)) {
                baseEntity.setParentid("0");
            }
            Date date = new Date();
            baseEntity.setUpdatetime(date);
            baseEntity.setCreatedate(date);
            //category 默认10
            if (null == baseEntity.getCategory() || "".equals(baseEntity.getCategory())){
                baseEntity.setCategory("10");
            }
            fpsPerMasterDataService.insertFpsPerMasterData(baseEntity);
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SUCCESS);
            resultDTO.setReturnMsg("新增成功");
        }catch(Exception e){
            e.printStackTrace();
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_SYSTEM_FAIL);
            return resultDTO;
        }
        return resultDTO;
    }

    /**
     * 远程更新组织结构
     * @param baseEntity
     * @return
     */
    @RequestMapping(value = "/remoteUpdateMaster.xhdx",method= RequestMethod.GET)
    @ResponseBody
    public ResultDTO<?> update(FpsPerMasterData baseEntity){
        ResultDTO<FpsPerMasterData> resultDTO = new ResultDTO<>();
        if(baseEntity==null){
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_PARAMETER_NULL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_PARAMETER_NULL);
            return resultDTO;
        }
        String id = baseEntity.getId();
        String name = baseEntity.getName();
        String code = baseEntity.getCode();
        if (null == id || "".equals(id)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("id为不能空，必须输入！");
            return resultDTO;
        }
        if (null == name || "".equals(name)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("name不能为空，必须输入！");
            return resultDTO;
        }
        if (null == code || "".equals(code)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("code不能为空，必须输入！");
            return resultDTO;
        }
        try{
            FpsPerMasterData fpsPerMasterData = fpsPerMasterDataService.getFpsPerMasterDataById(baseEntity.getId());
            if (fpsPerMasterData == null){
                resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
                resultDTO.setReturnMsg("id为"+id+"的数据不存在，请查证！");
                return resultDTO;
            }
            FpsPerMasterData fmd = new FpsPerMasterData();
            fmd.setName(baseEntity.getName());
            fmd.setCode(baseEntity.getCode());
            List<FpsPerMasterData> fp = fpsPerMasterDataService.findFpsPerMasterData(fmd);
            if(fp!=null && fp.size()>0){
                String checkId = fp.get(0).getId();
                if(!id.equals(checkId)){
                    resultDTO.setReturnCode(HxConstants.RETURN_CODE_DATA_EXIST);
                    resultDTO.setReturnMsg("该name为"+name+"和该code为"+code+"的组织机构已存在，请查证");
                    return resultDTO;
                }
            }
            Date date = new Date();
            baseEntity.setUpdatetime(date);
            fpsPerMasterDataService.updateFpsPerMasterData(baseEntity);
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SUCCESS);
            resultDTO.setReturnMsg("更新成功");
        }catch(Exception e){
            e.printStackTrace();
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_SYSTEM_FAIL);
            return resultDTO;
        }
        return resultDTO;
    }

    /**
     * 查询符合条件的所有机构接口
     * @param baseEntity
     * @return
     */
    @RequestMapping(value = "/remoteFindMasterPage.xhdx",method= RequestMethod.GET)
    @ResponseBody
    public List<FpsPerMasterData> findFpsPerMasterData(FpsPerMasterData baseEntity){
        List<FpsPerMasterData> fpsPerMasterDataList = fpsPerMasterDataService.findFpsPerMasterData(baseEntity);
        return fpsPerMasterDataList;
    }

    /**
     * 远程删除组织结构
     * @param baseEntity
     * @return
     */
    @RequestMapping(value = "/remotedeleteMasterbynames.xhdx",method= RequestMethod.GET)
    @ResponseBody
    public ResultDTO<?> delete(FpsPerMasterData baseEntity){
        ResultDTO<FpsPerMasterData> resultDTO = new ResultDTO<FpsPerMasterData>();
        if(baseEntity==null){
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_PARAMETER_NULL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_PARAMETER_NULL);
            return resultDTO;
        }
        String names = baseEntity.getNames();
        if (null == names || "".equals(names)){
            resultDTO.setReturnCode(HxConstants.RETURN_MSG_DATA_NULL);
            resultDTO.setReturnMsg("参数names为不能空，必须输入！");
            return resultDTO;
        }
        String[] namesArray = names.split(",");
        List<String> idList = new ArrayList<>();
        for (int i = 0; i < namesArray.length; i++) {
            String name = namesArray[i];
            List<FpsPerMasterData> fpsPerMasterDataListByName = fpsPerMasterDataService.getFpsPerMasterDataListByName(name);
            if (fpsPerMasterDataListByName.size()>0){
                for (FpsPerMasterData fpsPerMasterData : fpsPerMasterDataListByName) {
                    String id = fpsPerMasterData.getId();
                    List<FpsPerMasterData> fpsPerMasterDataList = fpsPerMasterDataService.getFpsPerMasterDataListById(id);
                    for (FpsPerMasterData element : fpsPerMasterDataList) {
                        idList.add(element.getId());
                    }
                }
            }
        }
        Set<String> set = new HashSet<>();
        set.addAll(idList);
        try{
            for (String id : set) {
                fpsPerMasterDataService.deleteFpsPerMasterDataById(id);
            }
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SUCCESS);
            resultDTO.setReturnMsg("删除成功");
        }catch(Exception e){
            e.printStackTrace();
            resultDTO.setReturnCode(HxConstants.RETURN_CODE_SYSTEM_FAIL);
            resultDTO.setReturnMsg(HxConstants.RETURN_MSG_SYSTEM_FAIL);
            return resultDTO;
        }
        return resultDTO;
    }

}
